#include "Player.h"
#include "Server.h"

#include <iostream>

Player::Player(){
	_control = NULL;
	_object = NULL;
	_server = NULL;
	_mouseXBefore = _mouseYBefore = 0;
}

Player::Player( Server* server, boost::asio::ip::udp::endpoint endpoint, ControlMessage* control, PlayerMessage* object)
	: _senderEndpoint(endpoint ),
	_server( server )
	
{
	_control = control;
	_object = object;
	_actor = _server->getNxScene()->createActor( new NxOgre::Box( 1, 1, 1 ) );
	_actor->setGlobalPosition( NxOgre::Vec3( 0, 20, 0 ) );
}

Player::~Player()
{

}

void Player::update(){
/*
	_object->attitude = -std::fmodf( _control->mouseX * 0.01f, 2.0f * 3.1416f );
	std::cout << _object->attitude << std::endl;
	float z = cosf(_object->attitude);
	float x = sinf(_object->attitude);
	_object->positionZ += _control->moveZ * z;
	_object->positionX += _control->moveZ * x;
	if (_object->positionX < -20.0f) _object->positionX = -20.0f;
	if (_object->positionX > +20.0f) _object->positionX = +20.0f;
	if (_object->positionZ < -20.0f) _object->positionZ = -20.0f;
	if (_object->positionZ > +20.0f) _object->positionZ = +20.0f;
*/

	if( _mouseXBefore > _control->mouseX )
		_actor->addForce( NxOgre::Vec3( -1, 0, 0 ) );
	else
		_actor->addForce( NxOgre::Vec3( 1, 0, 0 ) );
	if( _mouseYBefore > _control->mouseY )
		_actor->addForce( NxOgre::Vec3( 0, -1, 0 ) );
	else
		_actor->addForce( NxOgre::Vec3( 0, 1, 0 ) );
	if( _control->moveZ )
		_actor->addForce( NxOgre::Vec3( 0, 0, 1 ) );

	_object->attitude = _actor->getGlobalOrientationQuat().w;
	_object->fX = _actor->getGlobalOrientationQuat().x;
	_object->fY = _actor->getGlobalOrientationQuat().y;
	_object->fZ = _actor->getGlobalOrientationQuat().z;
	_object->positionX = _actor->getGlobalPosition().x;
	_object->positionY = _actor->getGlobalPosition().y;
	_object->positionZ = _actor->getGlobalPosition().z;

	std::cout << _actor->getGlobalPosition().x << std::endl;
	std::cout << _actor->getGlobalPosition().y << std::endl;
	std::cout << _actor->getGlobalPosition().z << std::endl << std::endl;
}